package com.cc.receiver;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener;
import org.springframework.stereotype.Component;

import com.rabbitmq.client.Channel;

import static com.cc.constant.RabbitMQConstants.*;

@Component
public class RabbitMQReceiver implements ChannelAwareMessageListener {

    @RabbitListener(queues = DIRECT_QUEUE)
    public void receiveDirectMessage(Message message, Channel channel) throws Exception {
        System.out.println("Received Direct Message: " + new String(message.getBody()));
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }

    @RabbitListener(queues = TOPIC_QUEUE)
    public void receiveTopicMessage(Message message, Channel channel) throws Exception {
        System.out.println("Received Topic Message: " + new String(message.getBody()));
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }

    @RabbitListener(queues = FANOUT_QUEUE)
    public void receiveFanoutMessage(Message message, Channel channel) throws Exception {
        System.out.println("Received Fanout Message: " + new String(message.getBody()));

        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }

    @RabbitListener(queues = HEADERS_QUEUE)
    public void receiveHeadersMessage(Message message, Channel channel) throws Exception {
        System.out.println("Received Headers Message: " + new String(message.getBody()));
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }

    @RabbitListener(queues = DEAD_LETTER_QUEUE)
    public void receiveDeadLetterMessage(Message message, Channel channel) throws Exception {
        System.out.println("Received Dead Letter Message: " + new String(message.getBody()));
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }

    @Override
    public void onMessage(Message message, Channel channel) throws Exception {
        System.out.println("Received Message: " + new String(message.getBody()));
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }
}
